#ifndef FILTER_H
#define FILTER_H

#include <cstdio>
#include <vector>
#include "cursor.h"
#include "trace.h"

#define KFILTER         10
#define DISTANCEFILTER  11

using namespace std;

class Filter{
    protected:
        int id;

    public:
        Filter(){}
        ~Filter(){}
        
        int getId(){
            return this->id;
        }
        
        virtual bool isCursor(Cursor &cursor)=0;
        virtual void filtering(vector<Cursor> &cursors)=0;
};

class KFilter : public Filter{
    private:
        int k;
        int quantity;
    public:
        KFilter(int k){
            this->quantity = 0;
            this->k = k;
            this->id = KFILTER;
        }
        ~KFilter(){}
        
        bool isCursor(Cursor &cursor){
            this->quantity++;
            if ( (this->quantity - 1)% k == 0){
                return true;
            }
            else{
                return false;
            }
        }
        
        void filtering(vector<Cursor> &cursors){
            for (unsigned int i=0; i<cursors.size(); i++){
                if(i%k != 0){
                    cursors.erase(cursors.begin()+i);
                }
            }
        
        }
};

class DistanceFilter : public Filter{
    private:
        float threshold;
        int quantity;
    public:
        DistanceFilter(float threshold){
            this->threshold = threshold;
            this->id = DISTANCEFILTER;
        }
        ~DistanceFilter(){}
        
        bool isCursor(Cursor &cursor){
            if (cursor.getPressure() > this->threshold){
                return true;
            }
            else{
                return false;
            }
        }
        
        void filtering(vector<Cursor> &cursors){
            if (cursors.size() >= 2){
                vector<Cursor> cs;
                cs.push_back( cursors.back() );
                for (int i=0; i<cursors.size(); i++){
                    if (cs.back().distance(cursors[i]) > this->threshold){
                        cs.push_back(cursors[i]);
                    }
                }
                cursors.clear();
                cursors = cs;
            }
        }
};

#endif /* FILTER_H */

